package com.hrt.datasender.datastorage.strategy.deviceoperinfo;

import com.btcode.db.result.Row;
import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.orm.model.IModel;
import com.hrt.datasender.datadao.model.BcAccessParamModel;
import com.hrt.datasender.datadao.model.BcAccessTableModel;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BcAccessParamService;
import com.hrt.datasender.datadao.service.BcAccessTableService;
import com.hrt.datasender.datadao.service.BdSendRecordService;
import com.hrt.datasender.datastorage.StorageDataType;
import com.hrt.datasender.datastorage.strategy.IStorageStrategy;
import com.hrt.db.DBManager;

import java.util.*;

public class DeviceOperInfo implements IStorageStrategy {

    @Override
    public void init() {
        // TODO Auto-generated method stub

    }

    @Override
    public List<IModel> getStorageData(Calendar cal,List<String> parmList) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 获取准备好要发送的数据时间
     * 
     * @return
     */
    protected Long getReadySendDataTime(BcSendPlatformModel platform) {
        BcAccessTableModel models = BcAccessTableService.getInstance().getModel(platform);

        SQLParameterList pc = new SQLParameterList();

        Date startDate = getLastSendTime(platform);

        long firstDate = 0;

        String sql = "SELECT * FROM " + models.getDevicetable() + " WHERE " + models.getDatatime()
                + " > @startDate ORDER BY " + models.getDatatime() + " ASC LIMIT 0,1";

        pc.put("startDate", startDate);

        List<Row> list = DBManager.getAccessExecuter().queryList(sql, pc);

        if (list.size() < 1) {
            return null;
        }

        Date datetime = list.get(0).getDate("DATATIME");

        firstDate = datetime.getTime();

        return firstDate;
    }

    @Override
    public List<Row> getSendData(BcSendPlatformModel sendPlatForm) {
        Long readySendDatatime = getReadySendDataTime(sendPlatForm);

        if (readySendDatatime == null) {
            return null;
        }

        List<Row> result = getData(sendPlatForm, readySendDatatime);
        return result;
    }

    public List<Row> getData(BcSendPlatformModel sendPlatForm, Long readySendDatatime) {
        BcAccessTableModel models = BcAccessTableService.getInstance().getModel(sendPlatForm);

        List<BcAccessParamModel> paramModels = BcAccessParamService.getInstance().getList();

        Date datetime = new Date(readySendDatatime);

        List<Row> result = null;
        List<Row> dataResult = new ArrayList<>();

        String sql = "SELECT * FROM " + models.getDevicetable() + " WHERE " + models.getDatatime()
                + " >= @datetime ORDER BY " + models.getDatatime() + " ASC LIMIT 0,1";
        SQLParameterList pc = new SQLParameterList();
        pc.put("datetime", datetime);
        result = DBManager.getAccessExecuter().queryList(sql, pc);

        for (Row row : result) {

            for (int j = 0; j < paramModels.size(); j++) {
                String deviceParam = paramModels.get(j).getDeviceParam();
                if(deviceParam==null){
                    continue;
                }
                else{
                    deviceParam = deviceParam.toUpperCase();
                }
                //I12001 工作状态
                String i12001 = row.getSafeString(deviceParam + "_I12001");
                //I13001 测量量程
                String i13001 = row.getSafeString(deviceParam + "_I13001");
                //I13002 测量精度
                String i13002 = row.getSafeString(deviceParam + "_I13002");
                //I13003 COD测量间隔
                String i13003 = row.getSafeString(deviceParam + "_I13003");
                //I13004 消解温度(摄氏度)
                String i13004 = row.getSafeString(deviceParam + "_I13004");
                //I13005 消解时长(分钟)
                String i13005 = row.getSafeString(deviceParam + "_I13005");
                //I13006 校准时间
                String i13006 = row.getSafeString(deviceParam + "_I13006");
                //I13007 截距
                String i13007 = row.getSafeString(deviceParam + "_I13007");
                //I13008 斜率
                String i13008 = row.getSafeString(deviceParam + "_I13008");
                //I13009 测量检出限
                String i13009 = row.getSafeString(deviceParam + "_I13009");
                //I13010 吸光度
                String i13010 = row.getSafeString(deviceParam + "_I13010");

                //DATATIME
                Date dateTime = row.getDate(models.getDatatime());
                long time = dateTime.getTime();

                Row item = new Row();
                item.put("I12001", i12001);
                item.put("I13001", i13001);
                item.put("I13002", i13002);
                item.put("I13003", i13003);
                item.put("I13004", i13004);
                item.put("I13005", i13005);
                item.put("I13006", i13006);
                item.put("I13007", i13007);
                item.put("I13008", i13008);
                item.put("I13009", i13009);
                item.put("I13010", i13010);
                item.put("PARAM_CODE_BUS", paramModels.get(j).getParamCodeBus());
                item.put("DATATIME", time);
                dataResult.add(item);
            }
        }

        return dataResult;
    }

    /**
     * 获取最后读取的时间
     * 
     * @return
     */
    public Date getLastSendTime(BcSendPlatformModel platform) {
        Date result = BdSendRecordService.getInstance().getLastSendDate(getDataType(), platform.getIp(),
                platform.getPort());
        return result;
    }

    @Override
    public StorageDataType getDataType() {
        return StorageDataType.DEVICE;
    }

    @Override
    public String getNote() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getShowName() {
        // TODO Auto-generated method stub
        return "设备运行信息";
    }

    @Override
    public int getDataInterval() {
        return 0;
    }

    @Override
    public String getStoageTable() {
        return "BD_S1_HOUR";
    }
}
